using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace WindowsApplication
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        public void PrintBttn_Click(Object sender, EventArgs evt)
        {
            List<FrameworkElement> elementList = FindTag(this, "PrintItem");

            foreach (FrameworkElement item in elementList)
            {
                item.IsEnabled = false;
            }
        }

        //This method would not be nessasary as you can use querying in the API.
        //This is a recursive method to search all children of a UI element for a specific tag.
        //In reality you would want a more extensible solution so that you are not limited to tags
        //which would require reflection. This was diificult to implement in the API, but enables developers
        //the ability to skip these challenging but repeditive functions.
        private List<FrameworkElement> FindTag(DependencyObject element, String tag)
        {
            List<FrameworkElement> resultsList = new List<FrameworkElement>();

            if (element == null)
            {
                return resultsList;
            }

            IEnumerator item = LogicalTreeHelper.GetChildren(element).GetEnumerator();

            while (item.MoveNext())
            {
                //try to type cast (the as command tries to cast rather than throwing an exception).
                //If it is not type castable then current will be null.
                FrameworkElement current = item.Current as FrameworkElement;

                if (current != null)
                {
                    if (current.Tag != null)
                    {
                        if (current.Tag.ToString() == tag)
                        {
                            resultsList.Add(current);
                        }
                    }   
                }

                //recurse the visual tree to search the child elements also.
                resultsList.AddRange(FindTag(current, tag));
            }
            return resultsList;
        }

    }
}